package utils

import (
	"fmt"
	"github.com/shopspring/decimal"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func ParseStringSliceToUint64(s []string) []uint64 {
	iv := make([]uint64, len(s))
	for i, v := range s {
		iv[i], _ = strconv.ParseUint(v, 10, 64)
	}
	return iv
}

func Decimal(value float64, decimal int) float64 {
	s := "%" + fmt.Sprintf(".%df", decimal)
	value, _ = strconv.ParseFloat(fmt.Sprintf(s, value), 64)
	return value
}

func Round(val float64) int64 {
	if val < 0 {
		return int64(val - 0.5)
	}
	return int64(val + 0.5)
}

// Decimal4d5aPc 四舍五入无精度丢失
func Decimal4d5aPc(value float64, n int) float64 {

	value, _ = decimal.NewFromFloat(value).Round(int32(n)).Float64()

	return value
}

func ConvertNumToCny(num float64) string {
	strnum := strconv.FormatFloat(num*100, 'f', 0, 64)
	sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
	s := sliceUnit[len(sliceUnit)-len(strnum):]
	upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
	str := ""
	for k, v := range strnum[:] {
		str = str + upperDigitUnit[string(v)] + s[k]
	}
	reg, err := regexp.Compile(`零角零分$`)
	str = reg.ReplaceAllString(str, "整")

	reg, err = regexp.Compile(`零角`)
	str = reg.ReplaceAllString(str, "零")

	reg, err = regexp.Compile(`零分$`)
	str = reg.ReplaceAllString(str, "整")

	reg, err = regexp.Compile(`零[仟佰拾]`)
	str = reg.ReplaceAllString(str, "零")

	reg, err = regexp.Compile(`零{2,}`)
	str = reg.ReplaceAllString(str, "零")

	reg, err = regexp.Compile(`零亿`)
	str = reg.ReplaceAllString(str, "亿")

	reg, err = regexp.Compile(`零万`)
	str = reg.ReplaceAllString(str, "万")

	reg, err = regexp.Compile(`零*元`)
	str = reg.ReplaceAllString(str, "元")

	reg, err = regexp.Compile(`亿零{0, 3}万`)
	str = reg.ReplaceAllString(str, "^元")

	reg, err = regexp.Compile(`零元`)
	str = reg.ReplaceAllString(str, "零")
	if err != nil {
		return ""
	}
	if str == "整" {
		return "零"
	}
	return str
}

func NumbersToString(numbers []*uint64) string {
	var numberStrings []string
	for _, number := range numbers {
		numberStrings = append(numberStrings, strconv.FormatUint(*number, 10))
	}
	return strings.Join(numberStrings, ",")
}

func FloatToString(number float64) string {
	return strconv.FormatFloat(number, 'f', -1, 64)
}

func CalcDatetime(input string) string {
	//input := "202305"  // 输入的年月
	layout := "200601" // 输入的年月的格式

	t1, err := time.Parse(layout, input)
	if err != nil {
		fmt.Println("解析日期失败:", err)
		return ""
	}

	// 求出下个月的第一天
	nextMonth := t1.AddDate(0, 1, 0)
	firstDayOfNextMonth := time.Date(nextMonth.Year(), nextMonth.Month(), 1, 0, 0, 0, 0, nextMonth.Location())

	// 当月最后一天 = 下个月第一天的前一天
	lastDay := firstDayOfNextMonth.AddDate(0, 0, -1)
	output := lastDay.Format("2006/01/02")
	fmt.Println(output)
	return output
}

func DigitToString(hour int) string {
	return fmt.Sprintf("%02d:00", hour)
}

func IsNumeric(str string) bool {
	_, err := strconv.ParseFloat(str, 64)
	return err == nil
}
